/*---------------------------------------------------

CODE BY: LeErOy NeWmAn 90%
10% to WHORSHIPPER for is door blow (too lazy to make a other one)

dont be a fagget by saying: you stole this addon
before say shit like that LEARN LUA AND CHECK THE FUCKING CODE

----------------------------------------------------*/

local kicktime = 0.7

if !ConVarExists("kick_powerscale") then
    CreateConVar("kick_powerscale", '1', FCVAR_NOTIFY)
end

if !ConVarExists("kick_chancetoblowdoor") then
    CreateConVar("kick_chancetoblowdoor", '5', FCVAR_NOTIFY)
end

if !ConVarExists("kick_blowdoormulforce") then
    CreateConVar("kick_blowdoormulforce", '1', FCVAR_NOTIFY)
end

if !ConVarExists("kick_blowdoorforce") then
    CreateConVar("kick_blowdoorforce", '300', FCVAR_NOTIFY)
end

if !ConVarExists("kick_blowdoor") then
    CreateConVar("kick_blowdoor", '1', FCVAR_NOTIFY)
end

if !ConVarExists("kick_effect") then
    CreateConVar("kick_effect", '1', FCVAR_NOTIFY)
end

if !ConVarExists("kick_maxdamage") then
    CreateConVar("kick_maxdamage", '35', FCVAR_NOTIFY)
end

if !ConVarExists("kick_mindamage") then
    CreateConVar("kick_mindamage", '20', FCVAR_NOTIFY)
end

if !ConVarExists("kick_physmul") then
    CreateConVar("kick_physmul", '5', FCVAR_NOTIFY)
end

if !ConVarExists("kick_hitshake") then
    CreateConVar("kick_hitshake", '1', FCVAR_NOTIFY)
end

if !ConVarExists("kick_hitragdollforce") then
    CreateConVar("kick_hitragdollforce", '100', FCVAR_NOTIFY)
end

if !ConVarExists("kick_doorrespawntime") then
    CreateConVar("kick_doorrespawntime", '25', FCVAR_NOTIFY)
end

if !ConVarExists("kick_damagebyspeed") then
    CreateConVar("kick_damagebyspeed", '1', FCVAR_NOTIFY)
end

if !ConVarExists("kick_damagebyspeeddiv") then
    CreateConVar("kick_damagebyspeeddiv", '10', FCVAR_NOTIFY)
end

function CalcPlayerModelsAngle( ply )
    
	if ply:Health() <= 0 then return end
	local StartAngle = ply:EyeAngles()
	if StartAngle == nil then return end
	local CalcAngle = Angle(0,StartAngle.y,0)
	if CalcAngle == nil then return end
	return CalcAngle
end

if CLIENT then

local WantedBone = {"ValveBiped.Bip01_R_Thigh", "ValveBiped.Bip01_R_Calf", "ValveBiped.Bip01_R_Foot"}

function CreatePlayerModels( ply )
    
	ply.CreatePlayerModelLegs = ClientsideModel(ply:GetModel(), RENDERGROUP_TRANSLUCENT)
	ply.CreatePlayerModelLegs:Spawn()
	ply.CreatePlayerModelLegs:SetPos(ply:GetPos())
	ply.CreatePlayerModelLegs:SetAngles(CalcPlayerModelsAngle( ply ))
	ply.CreatePlayerModelLegs:SetParent(ply)
	ply.CreatePlayerModelLegs:SetNoDraw(false)
	ply.CreatePlayerModelLegs:DrawModel()
	
end

/* -- test for changing the shitty gordon model to the legs of your current player model
function RemovePlayerModelsBone( legs )
    for k, v in ipairs( WantedBone ) do
	    local BoneMatrix = legs:GetBoneMatrix( legs:LookupBone( v ) )
		if BoneMatrix then
			BoneMatrix:Scale( vector_origin ) 
			legs:SetBoneMatrix( legs:LookupBone( v ), BoneMatrix )
		end
	end
end

function GetPlayerModels( ply )
    if ply.CreatePlayerModelLegs == nil then return end
	return ply.CreatePlayerModelLegs
end
*/

local function Kicking( um )
    if !LocalPlayer():Alive() then return false end
    if !LocalPlayer().StopKick then
        LocalPlayer().StopKick = CurTime() + 0.7
    elseif LocalPlayer().StopKick and LocalPlayer().StopKick < CurTime() then
        LocalPlayer().Kicking = um:ReadBool()
        LocalPlayer().KickTime = CurTime()
        LocalPlayer().StopKick = LocalPlayer().KickTime + 0.7
    end
end
usermessage.Hook( "Kicking", Kicking )

function CreateLegs()
for k, v in pairs(player.GetAll()) do

local Kicking = v.Kicking
    if GetViewEntity() == v and Kicking and v.StopKick and v.StopKick > CurTime() then
		if v.CreateLegs == nil then
			v.CreateLegs = ClientsideModel("models/weapons/v_kick.mdl", RENDERGROUP_TRANSLUCENT)
			v.CreateLegs:Spawn()
			v.CreateLegs:SetPos(v:GetShootPos())
			v.CreateLegs:SetAngles(v:EyeAngles())
			v.CreateLegs:SetParent(v)
			v.CreateLegs:SetNoDraw(false)
			v.CreateLegs:DrawModel()
			v.CreateLegs:SetPlaybackRate( 1 ) 
			v.CreateLegs.LastTick = CurTime()

			else
			v.CreateLegs:SetPos(v:GetShootPos())
		    v.CreateLegs:SetAngles(v:EyeAngles())
		    v.CreateLegs:SetModelScale(Vector(1, 1, 1))
			v.CreateLegs:FrameAdvance( CurTime() - v.CreateLegs.LastTick )
            v.CreateLegs:DrawModel()			
		    v.CreateLegs.LastTick = CurTime()
		end
	elseif v.CreateLegs and GetViewEntity() != v or Kicking and v.StopKick and v.StopKick < CurTime() then
		    v.CreateLegs:SetNoDraw(true)
	        v.CreateLegs:SetPos(Vector(0, 0, 0))
		    v.CreateLegs:SetAngles(Angle(0,0,0))
			v.CreateLegs:SetRenderOrigin(Vector(0, 0, 0))
	        v.CreateLegs:SetRenderAngles(Angle(0,0,0))
		    v.CreateLegs:SetModelScale(Vector(0, 0, 0))
			v.CreateLegs = nil
			v.Kicking = false
	end
end
end
hook.Add("Think","CreateLegs",CreateLegs)

end

function KickHit(ply)
   
    local trace = ply:GetEyeTrace()
	if trace == nil or trace.HitSky then return end
    local phys = trace.Entity:GetPhysicsObject()
	if phys == nil then return end
	
    local damage = math.random(GetConVarNumber("kick_mindamage"),GetConVarNumber("kick_maxdamage")) * GetConVarNumber("kick_powerscale")
	
	if GetConVarNumber("kick_damagebyspeed") >= 1 then
	    damage = damage + math.Clamp(ply:GetVelocity():Length() / GetConVarNumber("kick_damagebyspeeddiv"), 0, ply:GetVelocity():Length())
	end
	
	if ply:GetNWBool("Extention_Strength") then
	    damage = damage * 3
	end

    if SERVER then
    if trace.HitPos:Distance(ply:GetShootPos()) <= 85 then -- If we're in range
	    if GetConVarNumber("kick_hitshake") >= 1 then
		    local shake = ents.Create( "env_shake" )
		    shake:SetOwner(ply)
		    shake:SetPos( trace.HitPos )
		    shake:SetKeyValue( "amplitude", "2500" )
		    shake:SetKeyValue( "radius", "100" )
		    shake:SetKeyValue( "duration", "0.5" )
		    shake:SetKeyValue( "frequency", "255" )
		    shake:SetKeyValue( "spawnflags", "4" )	
		    shake:Spawn()
		    shake:Activate()
		    shake:Fire( "StartShake", "", 0 )
		end	
        if trace.Entity:IsPlayer() or string.find(trace.Entity:GetClass(),"npc") or string.find(trace.Entity:GetClass(),"prop_ragdoll") then	
	        if string.find(trace.Entity:GetClass(),"npc") and trace.Entity:Health() <= damage then
	            phys:ApplyForceOffset(ply:GetAimVector():GetNormalized() * (damage * GetConVarNumber("kick_physmul")), trace.HitPos)
	            trace.Entity:SetVelocity(ply:GetAimVector():GetNormalized() * (damage * GetConVarNumber("kick_physmul")))
			elseif string.find(trace.Entity:GetClass(),"prop_ragdoll") then
			    phys:ApplyForceOffset(ply:GetAimVector():GetNormalized() * ((damage * GetConVarNumber("kick_hitragdollforce") * GetConVarNumber("kick_physmul")) * GetConVarNumber("kick_powerscale")), trace.HitPos)
	        end
			//trace.Entity:EmitSound("player/smod_kick/foot_kickbody.wav", 100, math.random(80, 110))
			ply:ViewPunch( Angle( -10, math.random( -5, 5 ), 0 ) );
	        trace.Entity:TakeDamage(damage, ply, ply)
	    elseif trace.Entity:IsWorld() then
			//ply:EmitSound("player/smod_kick/foot_kickwall.wav", 100, math.random(70, 140))
			ply:ViewPunch( Angle( -10, math.random( -5, 5 ), 0 ) );
			 if GetConVarNumber("kick_effect") >= 1 then		
			    local fx 	= EffectData()
	            fx:SetStart(trace.HitPos)
	            fx:SetOrigin(trace.HitPos)
	            fx:SetNormal(trace.HitNormal)
	            util.Effect("kick_groundhit",fx)
	        end			
		elseif trace.Entity:GetClass() == "func_door_rotating" or trace.Entity:GetClass() == "prop_door_rotating" then
		    if math.random(1,GetConVarNumber("kick_chancetoblowdoor")) == 1 and GetConVarNumber("kick_blowdoor") >= 1 and trace.Entity:GetClass() == "prop_door_rotating" then
			    FakeDoor(trace.Entity, ply, damage)
				//ply:EmitSound("ambient/materials/door_hit1.wav", 100, math.random(80, 120))
	            ply:ViewPunch( Angle( -10, math.random( -5, 5 ), 0 ) );
			else	
		       //ply:EmitSound("ambient/materials/door_hit1.wav", 100, math.random(80, 120))
				trace.Entity:SetKeyValue( "Speed", "500" )
			    trace.Entity:Fire( "unlock", "", .01 )
	            trace.Entity:Fire( "open", "", .01 )
				timer.Simple(0.3, function()
				    trace.Entity:SetKeyValue( "Speed", "100" )
				end, trace.Entity)
	            ply:ViewPunch( Angle( -10, math.random( -5, 5 ), 0 ) );
			end
            if GetConVarNumber("kick_effect") >= 1 then		
			    local fx 	= EffectData()
	            fx:SetStart(trace.HitPos)
	            fx:SetOrigin(trace.HitPos)
	            fx:SetNormal(trace.HitNormal)
	            util.Effect("kick_groundhit",fx)
	        end			
		elseif trace.Entity:IsValid() then	
	        phys:ApplyForceOffset(ply:GetAimVector():GetNormalized() * (damage * 100 * GetConVarNumber("kick_physmul")), trace.HitPos)
	        trace.Entity:SetVelocity(ply:GetAimVector():GetNormalized() * (damage * 100 * GetConVarNumber("kick_physmul")))
			//ply:EmitSound("player/smod_kick/foot_kickwall.wav", 100, math.random(80, 110))
			ply:ViewPunch( Angle( -10, math.random( -5, 5 ), 0 ) );
	        trace.Entity:TakeDamage(damage, ply, ply)	
	    end 
	
	else
	    //ply:EmitSound("player/smod_kick/foot_fire.wav", 100, math.random(70, 110))
		ply:ViewPunch( Angle( -10, math.random( -5, 5 ), 0 ) );
	end
    end
end
-- thanks worshipper 8D
function FakeDoor(Door, attacker, amount)

        local pos = Door:GetPos()
		local ang = Door:GetAngles()
		local model = Door:GetModel()
		local skin = Door:GetSkin()

		Door:SetNotSolid(true)
		Door:SetNoDraw(true)

		local function ResetDoor(door, fakedoor)
		if door:IsValid() then
		local mass = door:GetNWInt("DoorHealthMaxHealth")
			door:SetNotSolid(false)
			door:SetNoDraw(false)
			door.DoorHealth = mass
	        door:SetNWInt("DoorHealth", door.DoorHealth )
			end
			if fakedoor:IsValid() then
			fakedoor:Remove()
			end
		end

		local ent = ents.Create("prop_physics")
		ent:SetPos(pos)
		ent:SetAngles(ang)
		ent:SetModel(model)
		if skin then
			ent:SetSkin(skin)
		end
		ent:Spawn()
		//ent:EmitSound("physics/wood/wood_furniture_break"..math.random(1,2)..".wav", 100, math.random(70, 140))
		ent:SetVelocity(attacker:GetAimVector() * (amount * GetConVarNumber("kick_blowdoorforce")) * GetConVarNumber("kick_blowdoormulforce"))
		ent:GetPhysicsObject():ApplyForceCenter(attacker:GetAimVector() * (amount * GetConVarNumber("kick_blowdoorforce")) * GetConVarNumber("kick_blowdoormulforce"))
		
		timer.Simple(GetConVarNumber("kick_doorrespawntime"), ResetDoor, Door, ent)
		
end

if (SERVER) then 

function KickingComm(ply)

    if !ply:Alive() then return false end
    if ply.StopKick and ply.StopKick < CurTime() then
        ply.Kicking = true
        ply.KickTime = CurTime()
        ply.StopKick = ply.KickTime + kicktime
        umsg.Start("Kicking",ply)
        umsg.Bool(true)
        umsg.End()
		timer.Simple(0.15, function()
            KickHit(ply)
		end, ply)
    end
end
concommand.Add("KickingComm",KickingComm)

function KickPlayerStart(ply)
    ply.Kicking = false
    ply.KickTime = CurTime()
    ply.StopKick = ply.KickTime + kicktime
end
hook.Add("PlayerSpawn","KickPlayerStart",KickPlayerStart)

function KickPlayerDeath(ply)
    ply.Kicking = false
    ply.KickTime = CurTime()
    ply.StopKick = ply.KickTime + kicktime
end
hook.Add("PlayerDeath","KickPlayerDeath",KickPlayerDeath)

end
